package com.community.controller;

import com.community.service.ElderService;
import com.community.service.NoticeService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ElderDelete")
public class ElderDeleteController extends HttpServlet {
    private ElderService elderService = new ElderService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // 获取ElderID参数
            String idParam = request.getParameter("ID");
            if (idParam == null || idParam.isEmpty()) {
                request.setAttribute("error", "ElderID不能为空");
                request.getRequestDispatcher("error.jsp").forward(request, response);
                return;
            }

            // 转换ID为整数
            int elderId = Integer.parseInt(idParam);

            elderService.deleteElder(elderId);

            request.getSession().setAttribute("message", "删除成功");
            response.sendRedirect(request.getContextPath() + "/ElderOverview");
        } catch (NumberFormatException e) {
            // 处理非法ID格式
            request.setAttribute("error", "无效的elderID格式");
            request.getRequestDispatcher("error.jsp").forward(request, response);
        } catch (Exception e) {
            // 处理其他异常
            e.printStackTrace(); // 打印堆栈跟踪以便调试
            request.setAttribute("error", "删除elder时发生错误: " + e.getMessage());
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }
}